const user_headers = [
  {text: '用户名', value: 'username', sortable: true,},
  {text: '密码', value: 'password', sortable: false,},
  {text: '是否可用', value: 'enabled', sortable: false,},
  {text: '性别', value: 'sex', sortable: false,},
  {text: 'roles', value: 'roles', sortable: false,},
  {text: '最后登录时间', value: 'lastLoginTime', sortable: true,},
  {text: '', value: 'action', sortable: false,},// align: 'center',
]
export default {
  // name: 'user_mixin',
  data: () => ({
    user_label: '用户', //中文名(标题)
    user_loading: false,//正在请求数据loading
    user_new: {	//新建时的默认值
    },
    user_headers: user_headers,
    user_labels: zz.listToMap(user_headers, 'value', 'text'),//字段名
    user_page_param: {
      startTimes: new Date().zzaddDay(-7).zzdate(),
      endTimes: null,
    },
    user_pagein: {
      curr: 1,//当前页码
      size: 10,//一页几行
      sortProps: [],//排序字段 List<String>
      sortDesc: [],//是不是倒序 List<Boolean>
    },
    users: [],		//列表
    user_total: 0,//总行数
    user_dialog: false,//弹窗是否显示
    user_opt: null,//添加 / 编辑
    user_formold: {},//暂存
    user_form: {},	//当前对象
    // user_selected: [],
  }),
  computed: {},
  watch: {
    'user_pagein': {
      handler: function (newVal, oldVal) {
        this.userPage()
      },
      deep: true
    }
  },
  created() {
    this.asyncCreated()

  },
  methods: {
    async asyncCreated() {
      await this.userPage()
      // console.log('userList()', await this.userList({sex: '女'}))
    },
    //分页查询
    async userPage() {
      await this.zztry('user_loading', async () => {
        let page = await zz.page('user', {...this.user_pagein, ...this.user_page_param})
        this.users = page.content
        this.user_total = page.total
      })
    },
    async userList(param) {
      return await this.zztry('user_loading', async () => {
        return zz.list('user', param)
      })
    },
    async userOne() {
      await this.zztry('user_loading', async () => {
        this.user_form = await zz.one('user', this.user_form)
        this.users.unshift(this.user_form)
        this.user_dialog = false
      })
    },
    userToAdd() {
      this.user_form = {...this.user_new}
      this.user_opt = '添加'
      this.user_dialog = true
    },
    userToEdit(user) {
      this.user_formold = user
      this.user_form = {...user}
      this.user_opt = '编辑'
      this.user_dialog = true
    },
    async userAdd() {
      await this.zztry('user_loading', async () => {
        if (this.$refs.user_form.validate()) {
          this.user_form.id = await zz.add('user', this.user_form)
          this.users.unshift(this.user_form)
          this.user_dialog = false
        }
      })
    },
    async userDelete(user) {
      await this.zztry('user_loading', async () => {
        await zz.delete('user', user.id)
        this.users.remove(user)
      })
    },
    async userUpdate() {
      await this.zztry('user_loading', async () => {
        if (this.$refs.user_form.validate()) {
          await zz.update('user', this.user_form)
          let index = this.users.indexOf(this.user_formold)
          this.users.splice(index, 1, this.user_form)
          this.user_dialog = false
        }
      })
    },
  },
}
